﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mimoza.ObjectLayer;
using Mimoza.PluginLayer;
using Mimoza.Apps.Console.ViewModel;
using Mimoza.Apps.Console.Resources;

namespace Mimoza.Apps.Console.View
{
    public partial class UpdateView : BaseView
    {
        public UpdateView()
        {
            InitializeComponent();
        }

        public override void Init(BaseViewModel viewModel)
        {
            m_viewModel = viewModel;

            this.Dock = DockStyle.Fill;
            this.listView.ContextMenuStrip = this.contextMenuStrip;

            //m_viewModel.PropertyChanged += new PropertyChangedEventHandler(UpdateViewModel_PropertyChanged);
            m_viewModel.OnObjectChanged += new EventHandler<ObjectLayer_ObjectChangedEventArgs>(UpdateViewModel_OnObjectChanged);

            AddCommand2Context(m_viewModel, this.contextMenuStrip);
            
            FillUpdates();
        }

        private void FillUpdates()
        {       
            listView.Items.Clear();
            foreach (Mimoza.ObjectLayer.Update update in m_viewModel.Application.ObjectLayer.UpdateRepository.GetUpdates())
            {
                InsertUpdate(update);
            }
        }

        private void InsertUpdate(Mimoza.ObjectLayer.Update update)
        {
            ListViewItem item = new ListViewItem();
            ListViewItem.ListViewSubItem sub1 = new ListViewItem.ListViewSubItem();
            ListViewItem.ListViewSubItem sub2 = new ListViewItem.ListViewSubItem();
            ListViewItem.ListViewSubItem sub3 = new ListViewItem.ListViewSubItem();

            item.Tag = update;
            item.Text = update.Name;
            item.ImageIndex = 0;

            sub1.Text = update.Model;
            sub2.Text = update.Version;
            sub3.Text = update.OS;

            item.SubItems.Add(sub1);
            item.SubItems.Add(sub2);
            item.SubItems.Add(sub3);

            listView.Items.Add(item);
        }

        private void UpdateUpdate(Mimoza.ObjectLayer.Update update)
        {
            FillUpdates();
        }

        private void DeleteUpdate(Mimoza.ObjectLayer.Update update)
        {
            foreach (ListViewItem item in listView.Items)
            {
                if ((item.Tag as Update).ID == update.ID)
                {
                    listView.Items.Remove(item);
                    break;
                }
            }
        }

        private void UpdateViewModel_OnObjectChanged(object sender, ObjectLayer_ObjectChangedEventArgs e)
        {
            Mimoza.ObjectLayer.Update update = e.Object as Mimoza.ObjectLayer.Update;
            switch (e.EventType)
            {
                case ObjectLayerEventType.ObjectInserted:
                    Action<Mimoza.ObjectLayer.Update> action = InsertUpdate;
                    Invoke(action, update);
                    break;
                case ObjectLayerEventType.ObjectUpdated:
                    Action<Mimoza.ObjectLayer.Update> actionUpdate = UpdateUpdate;
                    Invoke(actionUpdate, update);
                    break;
                case ObjectLayerEventType.ObjectDeleted:
                    Action<Mimoza.ObjectLayer.Update> actionDelete = DeleteUpdate;
                    Invoke(actionDelete, update);
                    break;
            }
        }

        private void listView_Enter(object sender, EventArgs e)
        {
            m_viewModel.InFocus = true;
        }

        private void listView_Leave(object sender, EventArgs e)
        {
            m_viewModel.InFocus = false;
        }

        private void listView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (listView.SelectedItems.Count != 0)
            {
                if (m_viewModel != null)
                {
                    m_viewModel.Context = listView.SelectedItems[0].Tag;
                }
            }
        }
    }
}
